"""
SPU管理视图
"""
from django.db.models.query import QuerySet
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListAPIView
from goods.models import SPU, Brand, GoodsCategory
from meiduo_admin.serializers.spu_serializers import *
from meiduo_admin.paginations import MyPage

class GoodsCategoryView(ListAPIView):
    """
    可选的分类：总共3级
    """
    queryset = GoodsCategory.objects.all()
    serializer_class = SPUSpecificationSerializer

    def get_queryset(self):
        parent_id = self.kwargs.get('pk')
        if parent_id:
            # 筛选出2级3级
            return self.queryset.filter(parent_id=parent_id)
        # 筛选出1级
        return self.queryset.filter(parent=None)





class BrandsSimpleView(ListAPIView):
    """
    可选的品牌
    """
    queryset = Brand.objects.all()
    serializer_class = BrandModelListSerializer


class SPUModelView(ModelViewSet):
    """
    SPU管理：增删改查
    """
    queryset = SPU.objects.all()
    serializer_class = SPUModelSerializer
    pagination_class = MyPage

    def get_queryset(self):
        keyword = self.request.query_params.get('keyword')
        if keyword:
            return self.queryset.filter(name__contains = keyword)
        return self.queryset.all()